import 'dart:ffi';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart' show Scaffold, VoidCallback;
import 'package:get/get.dart';
import 'package:jydoctorflutter/utils/tools/toast_extension.dart';

import '../../../config/user_data.dart';
import '../../../net/api.dart';
import '../../../routes/routes.dart';
import '../../home_main/logic.dart' show HomeMainLogic, Home_mainLogic;
import '../../main_tabbar/logic.dart';
import '../../main_tabbar/view.dart';
import '../../mine/mine_home/logic.dart';
import '../../work/work_home/logic.dart';
import '../obtain_ticket_webPage.dart';
enum messType{
    msm,
    voice,
    onoe,
}
extension FruitExtension on messType {
  String get fruitName {
    switch (this) {
      case messType.msm:
        return 'sms';
      case messType.voice:
        return 'voice';
      case messType.onoe:
        return '';
    }
  }
}

class SendCodeLogic extends GetxController {

  RxBool isselect = false.obs;
  RxBool isObtainYanZ = false.obs;
  RxBool isObtainYuYo = false.obs;
  messType choosetype = messType.onoe;
  String telephoneNUm = "";
  RxString ticketStr = ''.obs;
  RxString randstr = ''.obs;

  // 定义回调函数变量 ---类似block
  VoidCallback? widgetCallback;
  final phone = Get.arguments["telephone"] ?? '';
  void registerCallback(VoidCallback callback) {
    widgetCallback = callback;
  }

  @override
  void onInit() {
    print("来一次1");
    print("111==${ Get.arguments?["telephone"] ?? ''}");
    Get.lazyPut(() => MainTabbarLogic());
    Get.lazyPut(() => MineHomeLogic());
    Get.lazyPut(() => WorkHomeLogic());
    Get.lazyPut(() => HomeMainLogic());

    ever(ticketStr, (value) =>
    {
      print('当前result是: ${ticketStr}'),
      widgetCallback?.call(),

    });
    ever(randstr, (value) =>
    {
      print('当前result1是: ${randstr}'),
      sendCap()
    });
  }
  void sendCap(){
    print("验证失败=${ticketStr}");
    print("验证失败1=${randstr}");
    if (ticketStr.isEmpty){
      ('验证失败').toast();
      return ;
    }
    if (randstr.isEmpty){
      ('验证失败').toast();
      return ;
    }
    //获取验证码
    Map<String, dynamic> params = {
      "telephone": phone,
      "scenes":"login_n",
      "ticket":ticketStr.value,
      "randStr":randstr.value,
      "sendMode":choosetype.fruitName,
    };
    Api.sendCaptcha(params).then((onValue) async {
      ('发送成功').toast();
    }).catchError((onError) {
      "数据错误".toast();
    });
  }
  //点击下一步
  void clickGotoNext(){


    if (!isselect.value){
      ('请同意协议').toast();
      return ;
    }
   // 获取验证码
    Map<String, dynamic> params = {
      "telephone": phone,
      "captcha":telephoneNUm,
    };
    Api.loginByCaptcha(params).then((json) async {

      ('成功').toast();
      if (json.isNotEmpty) {
        if (json["accessToken"] != null) {
          await UserData.instance.saveToken(json["accessToken"]);
          Get.offAllNamed(Routes.maintabb);
        }
      }

    }).catchError((onError) {
      "数据错误123".toast();
    });

  }
  void gotoWeb(){
    // Get.to(obtainTicketWebPage());
    //全屏设置--调用次方法
     Get.generalDialog(
         barrierDismissible: true,
         barrierLabel: "Dismiss", // 这里补上！
         pageBuilder: (context, animation, secondaryAnimation) {
             return Scaffold(
                 body: obtainTicketWebPage(), // 这里就是全屏了
             );
         },
     );
  }

  void clickSelect(){
    isselect.value = !isselect.value;
  }
  void backTO(){
    Get.back();
  }


}
